//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using ManagementStudioServices.Interfaces;
    using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities.Properties;
    using Microsoft.Practices.Unity;
    using SR = Microsoft.Research.DataLayer;

    /// <summary>
    /// The presenter for the left pane user tree.
    /// </summary>
    public class UserLeftPanePresenter : RegistryManagerLeftPaneItemPresenterBase
    {
        /// <summary>
        /// Gets or sets the root model.
        /// </summary>
        /// <value>The root model.</value>
        public override BaseRegMgrModel RootModel
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowLeftPanePresenter"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="leftPane">The left pane.</param>
        /// <param name="service">The service.</param>
        public UserLeftPanePresenter(IUnityContainer container, UserTree leftPane, IRegistryManagerService service)
            : base(container, service)
        {
            this.LeftPaneView = leftPane;
            leftPane.Model = this;
        }

        #region IRegistryManagerLeftPaneItemPresenter Members

        /// <summary>
        /// Gets or sets the search view.
        /// </summary>
        /// <value>The search view.</value>
        public override UIElement SearchView
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the left pane view.
        /// </summary>
        /// <value>The left pane view.</value>
        public override UIElement LeftPaneView
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public override void Initialize()
        {
            try
            {
                if (this.LeftPaneTreeItems.Count > 0)
                {
                    this.LeftPaneTreeItems.Clear();
                }

                this.BuildTree();
            }
            catch (TridentCustomException ex)
            {
                TridentMessageBox.ShowTridentErrorMessageBox(ex.Message);
            }
        }

        #endregion

        /// <summary>
        /// Builds the User Tree
        /// </summary>
        private void BuildTree()
        {
            try
            {
                Collection<SR.User> users = this.RegistryManagerService.GetAllUsers();
                UserViewModel baseUser = new UserViewModel();
                baseUser.Name = ManagementStudio.VirtualUserName;
                baseUser.Id = Guid.NewGuid();
                baseUser.NonVirtualUser = false;
                this.LeftPaneTreeItems.Add(baseUser);
                foreach (SR.User user in users)
                {
                    UserViewModel userModel = new UserViewModel(user, baseUser);
                    baseUser.Children.Add(userModel);
                }

                this.RootModel = baseUser;
            }
            catch (TridentCustomException ex)
            {
                TridentMessageBox.ShowTridentErrorMessageBox(ex.Message); 
            }
        }

        /// <summary>
        /// Refreshes this instance.
        /// </summary>
        public override void Refresh(BaseRegMgrModel selectedModel)
        {
            this.Initialize();
            if (selectedModel != null)
            {
                selectedModel = selectedModel.GetModelById(selectedModel, this.LeftPaneTreeItems);
                if (selectedModel != null)
                {
                    base.ItemSelectedCommand.Execute(selectedModel);
                }
                else if(RootModel!= null)
                {
                    base.ItemSelectedCommand.Execute(RootModel);
                }
            }
        }
    }
}